package com.tuling.mybaitis_spring;

import com.tuling.mybaitis_spring.mapper.UserMapper;
import com.tuling.mybaitis_spring.proxy.MybaitisProxyFactoryBean;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.classreading.MetadataReader;

import java.io.IOException;
import java.util.Set;

public class ZhouyuClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {

	public ZhouyuClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
		super(registry);
	}

	@Override
	protected Set<BeanDefinitionHolder> doScan(String... basePackages) {

		Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(basePackages);

		for(BeanDefinitionHolder beanDefinitionHolder: beanDefinitionHolders){
			BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
			beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition.getBeanClassName());
//			registry.registerBeanDefinition(beanDefinition.getBeanClassName(),beanDefinition);
			beanDefinition.setBeanClassName(MybaitisProxyFactoryBean.class.getName());
		}

//		AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();

		return beanDefinitionHolders;
	}

	@Override
	protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
		//如果是一个接口就返回true
		return beanDefinition.getMetadata().isInterface();
	}

	@Override
	protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
		return true;
	}
}
